package com.demo.dbcompare.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.demo.dbcompare.model.vo.DbCompareDiffVo;

public class CompareUtils {
	public static final List<TableColumnInfo> defaultDiffColumnlist = Arrays.asList(new TableColumnInfo(),new TableColumnInfo());

	private CompareUtils(){}
	
	/**
	 * 取两个集合的交集
	 * @param left
	 * @param right
	 * @return
	 * @author: jie.deng
	 * @time: 2018年1月11日 上午9:38:40
	 */
	public static List<String> intersect(Collection<String> left, Collection<String> right) {
		Set<String> intersect = new HashSet<>();
		intersect.addAll(left);
		intersect.retainAll(right);
		return new ArrayList<>(intersect);
	}

	/**
	 * 取两个集合的差集
	 * @param leftList
	 * @param rightList
	 * @return
	 * @author: jie.deng
	 * @time: 2018年1月11日 上午9:56:29
	 */
	public static Set<String> subtract(List<String> leftList, List<String> rightList) {
		Set<String> subtract = new HashSet<>();
		subtract.addAll(leftList);
		subtract.removeAll(rightList);
		return subtract;
	}
	
	public static List<DbCompareDiffVo> genDefaultTableDiff(String connNameLeft, String connNameRight, String tableNameLeft,
			String tableNameRight) {
		if ((StringUtils.isBlank(tableNameLeft) && StringUtils.isBlank(tableNameRight))
				|| StringUtils.isNotBlank(tableNameLeft) && StringUtils.isNotBlank(tableNameRight)) {
			return Arrays.asList();
		}
		DbCompareDiffVo dbCompareDiff = new DbCompareDiffVo();
		dbCompareDiff.setConnNameLeft(connNameLeft);
		dbCompareDiff.setConnNameRight(connNameRight);
		dbCompareDiff.setColumnName("");
		dbCompareDiff.setCompareColumnList(defaultDiffColumnlist);
		if (StringUtils.isNotBlank(tableNameLeft)) {
			dbCompareDiff.setTableNameLeft(tableNameLeft);
			dbCompareDiff.setTableNameRight("");
		} else {
			dbCompareDiff.setTableNameLeft("");
			dbCompareDiff.setTableNameRight(tableNameRight);
		}
		return Arrays.asList(dbCompareDiff);
	}
}
